<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function like_drawing($user_id, $favorite_id) {
    if(!$user_id || !$favorite_id)
        return FALSE;
    $timestamp = time();
    
    $query = "INSERT INTO drawing_favorites(user_id, favorite_id, timestamp) VALUES ('$user_id', '$favorite_id', '$timestamp')";
    
    if(safe_insert_query($query)) {
        $query = "UPDATE drawings SET likes = likes + 1 WHERE id = '$favorite_id'";
        return safe_update_query($query);
    }
    
    return FALSE;
}

function unlike_drawing($user_id, $favorite_id) {
    if(!$user_id || !$favorite_id)
        return FALSE;
    
    $query = "DELETE FROM drawing_favorites WHERE user_id = '$user_id' AND favorite_id = '$favorite_id'";
    if(safe_delete_query($query)) {
        $query = "UPDATE drawings SET likes = likes - 1 WHERE id = '$favorite_id'";
        return safe_update_query($query);
    }
    return FALSE;
    
}

function get_favorite_drawing_ids($user_id) {
    $query = "SELECT favorite_id FROM drawing_favorites WHERE user_id = '$user_id'";
    return safe_select_query($query);
}

function get_people_who_like_drawing_ids($user_id) {
    $query = "SELECT user_id FROM drawing_favorites WHERE favorite_id = '$user_id'";
    return safe_select_query($query);
}

function check_whiteboard_favorite($user_id, $object_id) {
    $query = "SELECT id FROM drawing_favorites WHERE favorite_id = '$object_id' AND user_id = '$user_id' LIMIT 0, 1";
    $result = safe_select_query($query);
    $result = $result[0];
    if($result) {
        return array(
            'guid'          => $result->id,
            'relationship'  => 'to'
        );
    } else {
        return array(
            'guid'          => -1,
            'relationship'  => 'none'
        );
    }
}
?>
